package com.glw.sso.platform.web.controller;

import com.glw.sso.platform.common.base.BaseController;
import com.glw.sso.platform.common.group.Insert;
import com.glw.sso.platform.common.response.Response;
import com.glw.sso.platform.common.util.SHA1;
import com.glw.sso.platform.domain.entity.User;
import com.glw.sso.platform.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;

@Api
@Slf4j
@RestController
@RequestMapping("/api/user")
@Validated
public class UserController extends BaseController<UserService, User> {

    @Autowired
    UserService userService;

    @Override
    protected UserService getService() {
        return userService;
    }

    @Override
    public Response save(@RequestBody @Validated(Insert.class) User entity) {
        entity.setPassword(SHA1.encryption(entity.getPassword()));
        return super.save(entity);
    }

    @Operation(summary = "getUserRoutes")
    @GetMapping("getUserRoutes")
    public Response getUserRoutes(
            @ApiParam("应用ID")
            @NotEmpty(message = "应用ID不能为空") String appId) {
        return Response.succeeded(getService().getMenuTree(appId));
    }
}
